/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-03-21 10:42:15
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-05-15 15:48:22
 * @FilePath: /XMate-h5/src/store/modules/interviewBot.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import {
  getResumeApi,
  getInterviewInfoApi,
  getInterviewReportsApi,
  getQuestionListApi
} from "@/services/interviewBot";

export const useInterviewStore = defineStore("interviewStore", {
  state: () => ({
    questions: null,
    fileName: "",
    sharePath: "",
    images: [],
    interviewInfo: {},
    reportInfo: {},
    confId: null,
    isLoadingQuestion: false
  }),
  actions: {
    setConfId(confId) {
      this.confId = confId;
    },

    async getResumeAction() {
      const data = await getResumeApi(this.confId);
      if (!data) return;
      this.fileName = data.fileName;
      this.sharePath = data.sharePath;
      this.images = data.images;
      if (!data || !data.questions) {
        this.getQuestionListAction();
        return;
      } else {
        this.questions = data.questions;
      }

      return data;
    },

    async getInterviewInfoAction() {
      const data = await getInterviewInfoApi(this.confId);
      this.interviewInfo = data;
    },

    async getQuestionListAction() {
      this.isLoadingQuestion = true;
      this.timer = setInterval(async () => {
        if (!this.sharePath) return;
        const data = await getQuestionListApi(this.confId);
        if (!data || JSON.stringify(data) === "{}") return;
        if (this.timer) clearInterval(this.timer);
        this.timer = null;
        this.isLoadingQuestion = false;
        this.questions = data;
      }, 4000);
    },

    async getInterviewReportsAction() {
      const data = await getInterviewReportsApi(this.confId);
      this.reportInfo = data;
    }
  }
});
